home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / leda / incl / graph.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-05  |  24.0 KB  |  729 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  graph.h
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef GRAPHH
  16. #define GRAPHH
  17.  
  18. /*------------------------------------------------------------------------------
  19.  
  20.     Graph
  21.  
  22.     Stefan Naeher
  23.     FB10 Informatik 
  24.     Universitaet des Saarlandes 
  25.     6600 Saarbruecken
  26.  
  27.     September 1988
  28.     
  29. ------------------------------------------------------------------------------*/
  30.  
  31.  
  32. #include <LEDA\basic.h>
  33. #include <LEDA\list.h>
  34.  
  35.  
  36. //------------------------------------------------------------------------------
  37. /* declare node, edge, list(node) and list(edge)                              */
  38. //------------------------------------------------------------------------------
  39.  
  40. class i_node;
  41. class i_edge;
  42.  
  43. // node = pointer to internal node
  44. typedef i_node* node;
  45.  
  46. // edge = pointer to internal edge
  47. typedef i_edge* edge;
  48.  
  49.  
  50. /* nodelist = doubly linked list of nodes */
  51. declare(list,node)
  52.  
  53. /* list(edge) = doubly linked list of edges */
  54. declare(list,edge)
  55.  
  56.  
  57.  
  58. //------------------------------------------------------------------------------
  59. // class i_node: internal representation of nodes
  60. //------------------------------------------------------------------------------
  61.  
  62. class i_node {  
  63.  
  64.    friend class graph;
  65.    friend class ugraph;
  66.    friend class planar_map;
  67.    
  68.    friend graph* graph_of(edge);
  69.  
  70.    graph* g;             // pointer to graph 
  71.    int i_name;           // internal name (index)  
  72.    list_item loc;        // location in V
  73.    int indegree;        
  74.    ent contents;         // user defined pointer-type
  75.    list(edge) adj_edges;   /* list of adjacent edges */ 
  76.  
  77.    i_node(ent i)
  78.    { 
  79.      contents=i;
  80.      i_name = -1; 
  81.      g = 0; 
  82.      loc = 0; 
  83.      indegree = 0;
  84.    }
  85.  
  86.    OPERATOR_NEW(10)  
  87.    OPERATOR_DEL(10)
  88.  
  89. friend graph* graph_of(node v)   { return v->g;         }
  90. friend int    indeg(node v)      { return v->indegree;  }
  91. friend int    outdeg(node v)     { return v->adj_edges.length(); }
  92. friend int    degree(node v)     { return v->adj_edges.length(); }
  93. friend int    index(node v)      { return v->i_name;    }
  94.  
  95. /* space :  5 words + 1 list = 20 + 20 bytes = 40 bytes */
  96. };
  97.  
  98.  
  99. //------------------------------------------------------------------------------
  100. // class i_edge: internal representation of edges
  101. //------------------------------------------------------------------------------
  102.  
  103. class i_edge { 
  104.    
  105.    friend class graph;
  106.    friend class ugraph;
  107.    friend class planar_map;
  108.  
  109.    friend graph* graph_of(edge);
  110.  
  111.    int i_name;           // internal name (index)  
  112.    node s;               // edge from "source" 
  113.    node t;               // to "target"
  114.    list_item loc;        // location in |E|
  115.    list_item adj_loc;    // location in adjacencylist of source(e)
  116.    list_item adj_loc2;   // location in adjacencylist of target(e) (ugraph)
  117.    ent contents;         // user defined pointer type
  118.  
  119.    i_edge(node v, node w, ent i)
  120.    { contents=i;
  121.      i_name=-1;
  122.      s=v;
  123.      t=w;
  124.      loc=0;
  125.      adj_loc=0;
  126.      adj_loc2=0;
  127.    }
  128.  
  129.    OPERATOR_NEW(7)  
  130.    OPERATOR_DEL(7)
  131.  
  132. friend node   source(edge e) { return e->s;         }
  133. friend node   target(edge e) { return e->t;         }
  134. friend int    index(edge e)  { return e->i_name;    }
  135.  
  136. //space :  7 words = 28 bytes
  137. };
  138.  
  139.  
  140. inline graph* graph_of(edge e)   { return e->s->g;      }
  141.  
  142.  
  143.  
  144. typedef int  (*COMPARE(graph,node)) (node&, node&);
  145. typedef int  (*COMPARE(graph,edge)) (edge&, edge&);
  146.  
  147.  
  148. //------------------------------------------------------------------------------
  149. // Macro zur Definition von Feldern mit Indextyp node oder edge
  150. //------------------------------------------------------------------------------
  151.  
  152. // declaration (itype = node/edge):
  153.  
  154. #define graph_array(itype) name2(itype,graph_array)
  155.  
  156. #define graph_arraydeclare(itype)\
  157. \
  158. class graph_array(itype) {\
  159. graph* g;  /* array is declared for graph *g */  \
  160. int max_i; /* maximal node index in g at declaration time */  \
  161. ent* arr;  \
  162. virtual void clear_entry(ent& x) const { x=0; }\
  163. virtual void copy_entry(ent& x)  const { x=x; }\
  164. virtual void init_entry(ent& x)  const { x=0; }\
  165. \
  166. public:\
  167. virtual int cmp_entry(itype x, itype y) const { return int(inf(x))-int(inf(y)); }\
  168. \
  169.  ent& entry(itype);\
  170.  ent  inf(itype) const;\
  171.  void init(const graph&, int, ent);\
  172.  void init(const graph&, ent);\
  173.  void init(const graph_array(itype)&);\
  174.  void clear();\
  175.  ent& elem(int i)                    { return arr[i]; }\
  176.  int  size()               const     { return max_i+1;}\
  177.  graph_array(itype)()                { g=0; max_i=(-1); arr=0;}\
  178. ~graph_array(itype)()                { clear(); }\
  179. };\
  180.  
  181.  
  182. declare(graph_array,node)
  183. declare(graph_array,edge)
  184.  
  185.  
  186. //------------------------------------------------------------------------------
  187. // graph: base class for all graphs
  188. //------------------------------------------------------------------------------
  189.  
  190. class graph {
  191.  
  192. friend class ugraph;
  193. friend class planar_map;
  194.  
  195. list(node) V;              /* list of all nodes */
  196. list(edge) E;              /* list of all edges */
  197.  
  198. int max_node_index;      // maximal node index 
  199. int max_edge_index;      // maximal edge index
  200.  
  201. graph* parent;           // for subgraphs
  202.  
  203. bool   undirected;       // true iff graph undirected
  204.  
  205. /* space: 2 lists + 4 words + "virtual" = 2*20 + 5*4 bytes = 60 bytes */
  206.  
  207. virtual void init_node_entry(ent& x)  const { x=0; }
  208. virtual void init_edge_entry(ent& x)  const { x=0; }
  209.  
  210. virtual void copy_node_entry(ent& x)  const { x=x; }
  211. virtual void copy_edge_entry(ent& x)  const { x=x; }
  212.  
  213. virtual void clear_node_entry(ent& x) const { x=0; }
  214. virtual void clear_edge_entry(ent& x) const { x=0; }
  215.  
  216. virtual void read_node_entry(istream& in, ent& x)  const { Read(int(x),in); }
  217. virtual void read_edge_entry(istream& in, ent& x)  const { Read(int(x),in); }
  218.  
  219. virtual void write_node_entry(ostream& o, ent& x)  const { Print(int(x),o); }
  220. virtual void write_edge_entry(ostream& o, ent& x)  const { Print(int(x),o); }
  221.  
  222. virtual void print_node_entry(ostream&, ent&)  const {}
  223. virtual void print_edge_entry(ostream&, ent&)  const {}
  224.  
  225. virtual string node_type()  const { return "void"; }
  226. virtual string edge_type()  const { return "void"; }
  227.  
  228. protected:
  229.  
  230. void copy_all_entries();
  231.  
  232. public:
  233.  
  234. virtual int cmp_node_entry(node, node) const { return 0; }
  235. virtual int cmp_edge_entry(edge, edge) const { return 0; }
  236.  
  237.    int  space() const;
  238.    list(node) adj_nodes(node)   const;
  239.    void reset() const;
  240.  
  241.  
  242.    int  indeg(node v)     const;
  243.    int  outdeg(node v)    const;
  244.    node source(edge e)    const;
  245.    node target(edge e)    const;
  246.  
  247.    graph* super()         const;
  248.    int max_node_i()       const;
  249.    int max_edge_i()       const;
  250.  
  251.    int  number_of_nodes() const;
  252.    int  number_of_edges() const;
  253.  
  254.    list(node) all_nodes()       const;
  255.    list(edge) all_edges()       const;
  256.    list(edge) adj_edges(node v) const;
  257.  
  258.    ent& entry(node v);
  259.    ent& entry(edge e);
  260.    ent  inf(node v) const;
  261.    ent  inf(edge e) const;
  262.  
  263.    void assign(node v,ent x);
  264.    void assign(edge e,ent x);
  265.  
  266.  
  267.    void init_node_iterator() const;
  268.    void init_edge_iterator() const;
  269.    int  next_node(node& v)   const;
  270.    int  prev_node(node& v)   const;
  271.    int  next_edge(edge& e)   const;
  272.    int  prev_edge(edge& e)   const;
  273.  
  274.    edge first_adj_edge(node v)  const;
  275.    edge last_adj_edge(node v)   const;
  276.  
  277.    void init_adj_iterator(node v)        const;
  278.    int  next_adj_edge(edge& e,node v)    const ;
  279.    int  current_adj_edge(edge& e,node v) const ;
  280.    int  next_adj_node(node& w,node v)    const;
  281.    int  current_adj_node(node& w,node v) const;
  282.    
  283.    node first_node()      const;
  284.    edge first_edge()      const;
  285.    node last_node()       const;
  286.    edge last_edge()       const;
  287.    node choose_node()     const;
  288.    edge choose_edge()     const;
  289.    node succ_node(node v) const;
  290.    node pred_node(node v) const;
  291.    edge succ_edge(edge e) const;
  292.    edge pred_edge(edge e) const;
  293.    edge adj_succ(edge e)  const;
  294.    edge adj_pred(edge e)  const;
  295.    edge cyclic_adj_succ(edge e) const;
  296.    edge cyclic_adj_pred(edge e) const;
  297.  
  298.  
  299.    node new_node(ent);
  300.    void del_node(node);
  301.    void del_edge(edge);
  302.    edge new_edge(node, node, ent);
  303.    edge new_edge(edge, node, ent, int dir);
  304.  
  305.    node new_node()   { ent x; init_node_entry(x);  
  306.                        return new_node(x); }
  307.  
  308.    edge new_edge(node v, node w) 
  309.    { ent x; init_edge_entry(x);
  310.      return new_edge(v,w,x);}
  311.  
  312.    edge new_edge(edge e, node w, int dir=0) 
  313.    { ent x; init_edge_entry(x);
  314.      return new_edge(e,w,x,dir); }
  315.  
  316.  
  317.    void del_all_nodes(); 
  318.    void del_all_edges(); 
  319.  
  320.    list(edge) insert_reverse_edges();
  321.  
  322.    void sort_edges(COMPARE(graph,edge));
  323.    void sort_nodes(COMPARE(graph,node));
  324.  
  325.    void sort_edges(const graph_array(edge)&);
  326.    void sort_nodes(const graph_array(node)&);
  327.  
  328.    void sort_edges();
  329.    void sort_nodes();
  330.  
  331.    edge rev_edge(edge);
  332.    graph& rev();
  333.  
  334.    void make_directed();
  335.    void make_undirected();
  336.  
  337.    void write(string s) const;
  338.    int  read (string s);
  339.  
  340.    void print_node(node,ostream& = cout)  const;
  341.  
  342. virtual void print_edge(edge,ostream& = cout)  const;  //different for ugraph's
  343.  
  344.    void print(string s, ostream& = cout) const;
  345.  
  346.    void print()           const { print("");   }
  347.    void print(ostream& o) const { print("",o); }
  348.  
  349.    void clear();
  350.  
  351.    graph();
  352.    graph(const graph&);
  353.    graph& operator=(const graph&); 
  354.  
  355.   ~graph() { clear(); }
  356.  
  357.  
  358.    //subgraph constructors
  359.  
  360.    graph(graph&, list(node)&, list(edge)&);
  361.    graph(graph&, list(edge)&);
  362.  
  363. };
  364.  
  365. inline int  graph::indeg(node v)     const   { return v->indegree; }
  366. inline int  graph::outdeg(node v)    const   { return v->adj_edges.length(); }
  367. inline node graph::source(edge e)    const   { return e->s; }
  368. inline node graph::target(edge e)    const   { return e->t; }
  369.  
  370. inline graph* graph::super()         const   { return parent; }
  371. inline int graph::max_node_i()       const   { return max_node_index; }
  372. inline int graph::max_edge_i()       const   { return max_edge_index; }
  373.  
  374. inline int  graph::number_of_nodes() const   { return V.length(); }
  375. inline int  graph::number_of_edges() const   { return E.length(); }
  376.  
  377. inline ent& graph::entry(node v)        { return v->contents; }
  378. inline ent& graph::entry(edge e)        { return e->contents; }
  379. inline void graph::assign(node v,ent x) { v->contents = x; }
  380. inline void graph::assign(edge e,ent x) { e->contents = x; }
  381.  
  382. inline ent  graph::inf(node v) const { return v->contents; }
  383. inline ent  graph::inf(edge e) const { return e->contents; }
  384.  
  385.  
  386. inline void graph::init_node_iterator()   const { V.init_iterator(); }
  387. inline void graph::init_edge_iterator()   const { E.init_iterator(); }
  388. inline int  graph::next_node(node& v)     const { return V.next_element(v); }
  389. inline int  graph::prev_node(node& v)     const { return V.prev_element(v); }
  390. inline int  graph::next_edge(edge& e)     const { return E.next_element(e); }
  391. inline int  graph::prev_edge(edge& e)     const { return E.prev_element(e); }
  392. inline edge graph::first_adj_edge(node v) const { return v->adj_edges.head(); }
  393. inline edge graph::last_adj_edge(node v)  const { return v->adj_edges.tail(); }
  394.  
  395.  
  396. inline void graph::init_adj_iterator(node v) const 
  397. { v->adj_edges.init_iterator(); }
  398.  
  399. inline int  graph::next_adj_edge(edge& e,node v) const 
  400. { return v->adj_edges.next_element(e); }
  401.  
  402. inline int  graph::current_adj_edge(edge& e,node v) const 
  403. { return v->adj_edges.current_element(e);}
  404.  
  405.  
  406. inline int graph::next_adj_node(node& w,node v)  const
  407. { edge e;
  408.   if (next_adj_edge(e,v))
  409.   { w = (v==e->s) ? e->t : e->s;
  410.     return 1; 
  411.    }
  412.   else { w = 0; return 0;}
  413.  }
  414.    
  415. inline int graph::current_adj_node(node& w,node v)  const
  416. { edge e;
  417.   if (current_adj_edge(e,v))
  418.   { w = (v==e->s) ? e->t : e->s; 
  419.     return 1; }
  420.   else { w = 0; return 0; }
  421. }
  422.    
  423. inline node graph::first_node()   const { return V.head(); }
  424. inline edge graph::first_edge()   const { return E.head(); }
  425. inline node graph::last_node()    const { return V.tail(); }
  426. inline edge graph::last_edge()    const { return E.tail(); }
  427. inline node graph::choose_node()  const { return V.head(); }
  428. inline edge graph::choose_edge()  const { return E.head(); }
  429.  
  430. inline node graph::succ_node(node v)  const  
  431. { list_item loc = V.succ(v->loc);
  432.   return (loc) ? V.inf(loc) : 0;
  433.  }
  434.  
  435. inline node graph::pred_node(node v)  const  
  436. { list_item loc = V.pred(v->loc);
  437.   return (loc) ? V.inf(loc) : 0;
  438.  }
  439.  
  440. inline edge graph::succ_edge(edge e)  const  
  441. { list_item loc = E.succ(e->loc);
  442.   return (loc) ? E.inf(loc) : 0;
  443.  }
  444.  
  445. inline edge graph::pred_edge(edge e)  const  
  446. { list_item loc = E.pred(e->loc);
  447.   return (loc) ? E.inf(loc) : 0;
  448.  }
  449.  
  450.  
  451. inline edge graph::adj_succ(edge e) const 
  452. { list_item loc = e->s->adj_edges.succ(e->adj_loc);
  453.   return (loc) ? e->s->adj_edges[loc] : 0; 
  454.  } 
  455.  
  456. inline edge graph::adj_pred(edge e) const 
  457. { list_item loc = e->s->adj_edges.pred(e->adj_loc);
  458.   return (loc) ? e->s->adj_edges[loc] : 0; 
  459.  } 
  460.  
  461. inline edge graph::cyclic_adj_succ(edge e) const 
  462. { list_item loc = e->s->adj_edges.cyclic_succ(e->adj_loc);
  463.   return e->s->adj_edges[loc];
  464.  }
  465.  
  466. inline edge graph::cyclic_adj_pred(edge e)  const
  467. { list_item loc = e->s->adj_edges.cyclic_pred(e->adj_loc);
  468.   return e->s->adj_edges[loc];
  469.  }
  470.  
  471.  
  472.  
  473. //------------------------------------------------------------------------------
  474. // GRAPH: generic directed graphs
  475. //------------------------------------------------------------------------------
  476.  
  477. #define GRAPH(vtype,etype)   name3(vtype,etype,GRAPH)
  478.  
  479. #define GRAPHdeclare2(vtype,etype)\
  480. class GRAPH(vtype,etype) : public graph {\
  481. \
  482. void init_node_entry(ent& x) const  { x = Copy(vtype(0)); }\
  483. void init_edge_entry(ent& x) const  { x = Copy(etype(0)); }\
  484. \
  485. void copy_node_entry(ent& x) const  { Copy(*(vtype*)&x); }\
  486. void copy_edge_entry(ent& x) const  { Copy(*(etype*)&x); }\
  487. \
  488. void clear_node_entry(ent& x) const { Clear(*(vtype*)&x); }\
  489. void clear_edge_entry(ent& x) const { Clear(*(etype*)&x); }\
  490. \
  491. void read_node_entry(istream& i, ent& x)  const { Read(*(vtype*)&x,i); }\
  492. void read_edge_entry(istream& i, ent& x)  const { Read(*(etype*)&x,i); }\
  493. \
  494. void write_node_entry(ostream& o, ent& x)  const { Print(*(vtype*)&x,o); }\
  495. void write_edge_entry(ostream& o, ent& x)  const { Print(*(etype*)&x,o); }\
  496. \
  497. void print_node_entry(ostream& o, ent& x)  const\
  498.      { o << "("; Print(*(vtype*)&x,o); o << ")"; }\
  499. void print_edge_entry(ostream& o, ent& x)  const\
  500.      { o << "("; Print(*(etype*)&x,o); o << ")"; }\
  501. \
  502. string node_type()  const { return STRINGIZE(vtype); }\
  503. string edge_type()  const { return STRINGIZE(etype); }\
  504. \
  505. public:\
  506. \
  507. int cmp_node_entry(node x, node y) const { return compare(inf(x),inf(y)); }\
  508. int cmp_edge_entry(edge x, edge y) const { return compare(inf(x),inf(y)); }\
  509. \
  510.    vtype  inf(node v)    const   { return vtype(graph::inf(v)); }\
  511.    etype  inf(edge e)    const   { return etype(graph::inf(e)); }\
  512.    vtype& operator[] (node v)         { return *(vtype*)&graph::entry(v); }\
  513.    vtype  operator[] (node v) const   { return vtype(graph::inf(v)); }\
  514.    etype& operator[] (edge e)         { return *(etype*)&graph::entry(e); }\
  515.    etype  operator[] (edge e) const   { return etype(graph::inf(e)); }\
  516.    void   assign(node v,vtype x) { graph::assign(v,Ent(x)); }\
  517.    void   assign(edge e,etype x) { graph::assign(e,Ent(x)); }\
  518.    node   new_node()             { return graph::new_node(); }\
  519.    node   new_node(vtype a)      { return graph::new_node(Copy(a)); }\
  520.    edge   new_edge(node v, node w)\
  521.                               { return graph::new_edge(v,w); }\
  522.    edge   new_edge(node v, node w, etype a)\
  523.                               { return graph::new_edge(v,w,Copy(a)); }\
  524.    edge   new_edge(edge e, node w)\
  525.                               { return graph::new_edge(e,w); }\
  526.    edge   new_edge(edge e, node w, etype a)\
  527.                               { return graph::new_edge(e,w,Copy(a),0); }\
  528.    edge   new_edge(edge e, node w, etype a, int dir)\
  529.                               { return graph::new_edge(e,w,Copy(a),dir); }\
  530. \
  531.    GRAPH(vtype,etype)& operator=(const GRAPH(vtype,etype)& a)\
  532.        {return (GRAPH(vtype,etype)&)(graph::operator=((graph&)a)); }\
  533.    GRAPH(vtype,etype)()     {}\
  534.    GRAPH(vtype,etype)(const GRAPH(vtype,etype)& a) : graph((graph&)a) { copy_all_entries();} \
  535. /* sub graphs */\
  536.    GRAPH(vtype,etype)(GRAPH(vtype,etype)& a, list(node)& b, list(edge)& c)\
  537.                                                 : graph((graph&)a,b,c) {}\
  538.    GRAPH(vtype,etype)(GRAPH(vtype,etype)& a, list(edge)& c)\
  539.                                                 : graph((graph&)a,c) {}\
  540.    ~GRAPH(vtype,etype)()     { clear(); }\
  541. };
  542.  
  543. //------------------------------------------------------------------------------
  544. // node arrays
  545. //------------------------------------------------------------------------------
  546.  
  547. #define node_array(TYPE) name2(TYPE,node_array)
  548.  
  549. #define node_arraydeclare(type)\
  550. \
  551. class node_array(type) : public graph_array(node) {\
  552. void copy_entry(ent& x)  const { x = Copy(*(type*)&x);  }\
  553. void clear_entry(ent& x) const { Clear(*(type*)&x);     }\
  554. void init_entry(ent& x)  const { x = Copy(type(0)); }\
  555. \
  556. public:\
  557. int cmp_entry(node x,node y) const { return compare(operator[](x),operator[](y)); }\
  558. \
  559. type& operator[](node x)       { return *(type*)&graph_array(node)::entry(x);}\
  560. type  operator[](node x) const { return type(graph_array(node)::inf(x));}\
  561. type& elem(int i)           { return *(type*)&graph_array(node)::elem(i);}\
  562. void  init(const graph& G, int n, type i)\
  563.                                   { graph_array(node)::init(G,n,Ent(i)); }\
  564. void  init(const graph& G, type i){ graph_array(node)::init(G,Ent(i)); }\
  565. void  init(const graph& G)  { type y=0; graph_array(node)::init(G,Ent(y)); }\
  566. void  init(const node_array(type)& A)\
  567.                             { graph_array(node)::init((graph_array(node)&) A); }\
  568. \
  569. node_array(type)()                                 {}\
  570. node_array(type)(const graph& G)                   { init(G);   }\
  571. node_array(type)(const graph& G, int n, type i)    { init(G,n,i); }\
  572. node_array(type)(const graph& G, type i)           { init(G,i); }\
  573. node_array(type)(const node_array(type)& A)        { init(A);   }\
  574. node_array(type)& operator=(const node_array(type)& A) { init(A); return *this;}\
  575. \
  576. ~node_array(type)()                { clear(); }\
  577. };
  578.  
  579.  
  580. //------------------------------------------------------------------------------
  581. // edge arrays
  582. //------------------------------------------------------------------------------
  583.  
  584. #define edge_array(TYPE) name2(TYPE,edge_array)
  585.  
  586. #define edge_arraydeclare(type)\
  587. \
  588. class edge_array(type) : public graph_array(edge) {\
  589. void copy_entry(ent& x)  const { x = Copy(*(type*)&x);  }\
  590. void clear_entry(ent& x) const { Clear(*(type*)&x);     }\
  591. void init_entry(ent& x)  const { x = Copy(type(0)); }\
  592. \
  593. public:\
  594. int cmp_entry(edge x,edge y) const { return compare(operator[](x),operator[](y)); }\
  595. \
  596. type& operator[](edge x)       { return *(type*)&graph_array(edge)::entry(x);}\
  597. type  operator[](edge x) const { return type(graph_array(edge)::inf(x));}\
  598. type& elem(int i)           { return *(type*)&graph_array(edge)::elem(i);}\
  599. void  init(const graph& G, int m, type i)\
  600.                                   { graph_array(edge)::init(G,m,Ent(i)); }\
  601. void  init(const graph& G, type i){ graph_array(edge)::init(G,Ent(i)); }\
  602. void  init(const graph& G)  { type y=0; graph_array(edge)::init(G,Ent(y)); }\
  603. void  init(const edge_array(type)& A)\
  604.                             { graph_array(edge)::init((graph_array(edge)&) A); }\
  605. \
  606. edge_array(type)()                                 {}\
  607. edge_array(type)(const graph& G)                   { init(G);   }\
  608. edge_array(type)(const graph& G, int m, type i)    { init(G,m,i); }\
  609. edge_array(type)(const graph& G, type i)           { init(G,i); }\
  610. edge_array(type)(const edge_array(type)& A)        { init(A);   }\
  611. edge_array(type)& operator=(const edge_array(type)& A) { init(A); return *this;}\
  612. \
  613. ~edge_array(type)()                { clear(); }\
  614. };
  615.  
  616. //------------------------------------------------------------------------------
  617. // node matrices
  618. //------------------------------------------------------------------------------
  619.  
  620. typedef graph_array(node)* node_array_ptr;
  621.  
  622. declare(node_array,node_array_ptr);
  623.  
  624. class Node_Matrix {
  625. graph* g;
  626. node_array(node_array_ptr) M;
  627. virtual void init_entry(ent& x)  const { x=0; }
  628. virtual void copy_entry(ent& x)  const { x=x; }
  629. virtual void clear_entry(ent& x) const { x=0; }
  630. virtual void print_entry(ent& x) const { Print(x); }
  631. public:
  632.  graph_array(node)& operator[](node v);
  633.  ent& entry(node, node);
  634.  ent  inf(node, node) const;
  635.  void clear();
  636.  void init(const graph&, int, ent);
  637.  void init(const graph&, ent);
  638.  void init(Node_Matrix&);
  639.  Node_Matrix()  {}
  640. ~Node_Matrix()  { clear(); }
  641. };
  642.  
  643.  
  644. #define node_matrix(type) name2(type,node_matrix)
  645.  
  646. #define node_matrixdeclare(type)\
  647. \
  648. class node_matrix(type): public Node_Matrix {\
  649. void copy_entry(ent& x)  const { x = Copy(*(type*)&x);  }\
  650. void clear_entry(ent& x) const { Clear(*(type*)&x); }\
  651. void init_entry(ent& x)  const { x = Copy(type(0)); }\
  652. void print_entry(ent& x) const { Print(*(type*)&x); }\
  653. public:\
  654. node_array(type)& operator[](node v)\
  655. { return *(node_array(type)*)&(Node_Matrix::operator[](v)); }\
  656. \
  657. type& operator()(node v, node w) { return *(type*)&(Node_Matrix::entry(v,w));}\
  658. type  operator()(node v, node w) const { return type(Node_Matrix::inf(v,w)); }\
  659. \
  660. void  init(const graph& G, int n, type i) { Node_Matrix::init(G,n,Ent(i)); }\
  661. void  init(const graph& G, type i)        { Node_Matrix::init(G,Ent(i)); }\
  662. void  init(const graph& G)    { type y = 0; Node_Matrix::init(G,Ent(y)); }\
  663. void  init(const node_matrix(type)& M) { Node_Matrix::init((Node_Matrix&) M); }\
  664. \
  665. node_matrix(type)() {}\
  666. node_matrix(type)(const graph& G)                 { init(G);   }\
  667. node_matrix(type)(const graph& G, int n, type x)  { init(G,n,x); }\
  668. node_matrix(type)(const graph& G, type x)         { init(G,x); }\
  669. node_matrix(type)(const node_matrix(type)& M)     { init(M); }\
  670. \
  671. node_matrix(type)& operator=(const node_matrix(type)& M){init(M);return *this;}\
  672. \
  673. ~node_matrix(type)()                 { clear();   }\
  674. };\
  675.  
  676.  
  677. //------------------------------------------------------------------------------
  678. // Iteration   (macros)
  679. //------------------------------------------------------------------------------
  680.  
  681. /*
  682. #define forall_nodes(a,b) for ((b).init_node_iterator(); (b).next_node(a); )
  683. #define forall_edges(a,b) for ((b).init_edge_iterator(); (b).next_edge(a); )
  684. */
  685.  
  686. #define forall_nodes(a,g) for (a=(g).first_node(); a; a=(g).succ_node(a) )
  687.  
  688. #define forall_edges(a,g) for (a=(g).first_edge(); a; a=(g).succ_edge(a) )
  689.  
  690. #define forall_adj_edges(a,b)\
  691. for (graph_of(b)->init_adj_iterator(b); graph_of(b)->next_adj_edge(a,b); )
  692.  
  693. /*
  694. #define forall_adj_edges(a,b)\
  695.    for (a=graph_of(b)->first_adj_edge(b); a; a=graph_of(b)->adj_succ(a) ) */
  696.  
  697.  
  698. #define forall_adj_nodes(a,b)\
  699. for (graph_of(b)->init_adj_iterator(b); graph_of(b)->next_adj_node(a,b); )
  700.  
  701. #define forall_node_pairs(a,b,g)\
  702. forall_nodes(a,g) for (b=(g).first_node(); b; b=(g).succ_node(b) )
  703.  
  704.  
  705. // reverse
  706.  
  707. #define Forall_nodes(a,g) for (a=(g).last_node(); a; a=(g).pred_node(a) )
  708.  
  709. #define Forall_edges(a,g) for (a=(g).last_edge(); a; a=(g).pred_edge(a) )
  710.  
  711.  
  712.  
  713. //-----------------------------------------------------------------------------
  714. // miscellaneous  (should be members or constructors)
  715. //-----------------------------------------------------------------------------
  716.  
  717. extern void eliminate_parallel_edges(graph& G);
  718.  
  719. extern void complete_graph(graph&,int);
  720. extern void random_graph(graph&,int,int);
  721. extern void random_planar_graph(graph&,int);
  722.  
  723. extern void test_graph(graph&);
  724. extern void test_bigraph(graph&, list(node)&, list(node)&);
  725.  
  726. #endif
  727.  
  728.